package com.PuzGame.combinenumbers.customViews;

import android.content.Context;
import android.graphics.Color;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.Chronometer;

import com.PuzGame.combinenumbers.R;

public class CountUp extends Chronometer {

	Chronometer tv = this;
	public CountUp(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Init();
	}

	public CountUp(Context context, AttributeSet attrs) {
		super(context, attrs);
		Init();
	}
	
	public CountUp(Context context) {
		super(context);
		Init();
	}

	public void Init() {
		
		setOnChronometerTickListener(
				new Chronometer.OnChronometerTickListener(){

					@Override
					public void onChronometerTick(Chronometer chronometer) {
						// TODO Auto-generated method stub
						long millis = SystemClock.elapsedRealtime() - chronometer.getBase();

						long second = (millis / 1000) % 60;
						long minute = (millis / (1000 * 60)) % 60;
						long hour = (millis / (1000 * 60 * 60)) % 24;
						long day = (millis / (1000 * 60 * 60 * 24));

						String time = "";
						if(day == 0 && hour == 0)
						{
							time = String.format("%02d:%02d", minute ,second);
						}
						else if(day == 0)
						{
							time = String.format("%02d:%02d:%02d", hour, minute ,second);
						}
						else
						{
							time = String.format("%01d:%02d:%02d:%02d", day, hour, minute, second);
						}

						tv.setText(time);
						
						if(millis/1000 < 10)
						{
							tv.setTextColor(Color.GREEN);
							
						}
						else if(millis/1000 < 60)
						{
							tv.setTextColor(Color.parseColor("#CCFF33"));
						}
						else if(millis/1000 < 60*5)
						{
							tv.setTextColor(Color.YELLOW);
						}
						else if(millis/1000 < 60*10)
						{
							tv.setTextColor(Color.parseColor("#FFA500"));
						}
						else
						{
							tv.setTextColor(Color.RED);
						}

					}}
				);
		setBackgroundResource(R.drawable.backtimers);
	}

}
